<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right"
                   style="font-size: 18px">
      <el-breadcrumb-item :to="{ path: '/' }">后台管理</el-breadcrumb-item>
      <el-breadcrumb-item>专业列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>
    <el-form :model="ruleForm" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="院系" prop="facultyId">
        <el-select v-model="ruleForm.facultyId" placeholder="请选择院系">
          <el-option
              v-for="item in facultyListOptions"
              :key="item.id"
              :label="item.facultyName"
              :value="item.id">
          </el-option>
        </el-select>
        <span style="margin-left: 30px">
        <el-button type="primary" @click="submitForm('ruleForm')">查询</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
          </span>
      </el-form-item>
    </el-form>
    <el-table
            :data="majorArr"
            border
            style="width: 100%">
     <!-- <el-table-column prop="id" label="ID" width="70" align="center">
      </el-table-column>-->
      <el-table-column prop="majorName" label="专业名称" width="150" align="center">
      </el-table-column>
      <el-table-column prop="establishedTime" label="成立时间" width="150" align="center">
      </el-table-column>
      <el-table-column prop="num" label="学生人数" width="100" align="center">
      </el-table-column>
      <el-table-column prop="teacherName" label="负责老师" width="100" align="center">
      </el-table-column>
      <el-table-column prop="facultyName" label="所属院系" width="100" align="center">
      </el-table-column>
      <el-table-column label="操作" width="120" align="center">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" circle size="mini"
          @click="handleEdit(scope.row)"></el-button>
          <el-button type="danger" icon="el-icon-delete" circle size="mini"
                     @click="openDeleteConfirm(scope.row)"></el-button>
        </template>  <!--scope.row表示当前行-->
      </el-table-column>
    </el-table>

    <!-- 弹出的编辑专业的对话框 -->
    <el-dialog title="修改专业" :visible.sync="dialogFormVisible">
      <el-form :model="ruleForm">
        <el-form-item label="专业" :label-width="formLabelWidth">
          <el-input v-model="ruleForm.majorName"></el-input>
        </el-form-item>
        <el-form-item label="成立时间" :label-width="formLabelWidth">
          <template>
            <el-date-picker
                v-model="ruleForm.establishedTime"
                align="right"
                type="date"
                value-format="yyyy-MM-dd"
                format="yyyy-MM-dd"
                placeholder="选择日期">
            </el-date-picker>
          </template>
        </el-form-item>
        <el-form-item label="学生人数" :label-width="formLabelWidth">
          <el-input v-model="ruleForm.num"></el-input>
        </el-form-item>
        <el-form-item label="负责老师" :label-width="formLabelWidth">
          <el-select v-model="ruleForm.teacherName" placeholder="请选择">
            <el-option
                v-for="item in teacherListOptions"
                :key="item.id"
                :label="item.name"
                :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属院系" :label-width="formLabelWidth">
          <el-select v-model="ruleForm.facultyName" placeholder="请选择">
            <el-option
                v-for="item in facultyListOptions"
                :key="item.id"
                :label="item.facultyName"
                :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitEdit()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        majorArr: [],
        dialogFormVisible: false,
        /*facultyTableData: [],*/
        facultyListOptions: [],
        teacherListOptions: [],
        ruleForm: {
          facultyId: '',
          majorId:'',
          majorName:'',
          establishedTime:'',
          teacherName:'',
          facultyName:''
        },
        formLabelWidth: '120px'
      }
    },
    methods: {
      handleEdit(major){
        let message = '您正在尝试编辑【' + major.id + '-' + major.majorName + '】的专业详情……';
        console.log(message);
        let url = 'http://localhost:9801/majors/' + major.id;
        console.log(url);
        this.axios.get(url).then((response) => {
          let responseBody = response.data;
          if (responseBody.state == 20000) {
            this.ruleForm = responseBody.data;
            console.log(responseBody.data)
            this.dialogFormVisible = true;
          } else {
            this.$message.error(responseBody.message);
            this.loadMajorList();
          }
        });
      },
      submitEdit() {
        let url = 'http://localhost:9801/majors/' + this.ruleForm.id +'/update';
        console.log('url = ' + url);
        let formData = this.qs.stringify(this.ruleForm);
        console.log('formData = ' + formData);
        this.axios.post(url, formData).then((response) => {
          let responseBody = response.data;
          if (responseBody.state == 20000) {
            this.$message({
              message: '修改专业成功！',
              type: 'success'
            });
            this.dialogFormVisible = false;
            this.loadMajorList();
          } else if (responseBody.state == 40900) {
            this.$message.error(responseBody.message);
          } else {
            this.$message.error(responseBody.message);
            this.dialogFormVisible = false;
            this.loadMajorList();
          }
        });
      },
      handleDelete(major){
        console.log('准备执行删除, id=' + major.id);
        let url = 'http://localhost:9801/majors/' + major.id + '/delete';
        console.log('url=' + url);
        this.axios.post(url).then((response) => {
          let responseBody = response.data;
          if (responseBody.state != 20000){  //如果删除不成功则提示错误信息
            this.$message.error(responseBody.message);
          }
            this.loadMajorList();  //刷新页面
        })
      },
      openDeleteConfirm(major){
        let message = '此操作将永久删除【' + major.majorName+ '】专业, 是否继续?';
        this.$confirm(message, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.handleDelete(major);
        }).catch(() => {
          //点击取消后的操作
        });
      },
      loadFacultyList() {
        console.log('loadFacultyList');
        let url = 'http://localhost:9801/faculties';
        console.log('url = ' + url);
        this.axios.get(url).then((response) => {
          let responseBody = response.data;
          this.facultyListOptions = responseBody.data;
          /*this.list = this.facultyListOptions;*/
        });
      },
      loadMajorListByFacultyId() {
        console.log('loadMajorListByFacultyId');
        let url = 'http://localhost:9801/majors/faculty/' + this.ruleForm.facultyId;
        console.log('url = ' + url);
        this.axios.get(url).then((response) => {
          let responseBody = response.data;
          this.majorArr = responseBody.data;
        });
      },
      loadMajorList() {
        console.log('loadMajorList');
        let url = 'http://localhost:9801/majors';
        console.log('url = ' + url);
        this.axios.get(url).then((response) => {
          let responseBody = response.data;
          this.majorArr = responseBody.data;
        });
      },
      loadTeacherList() {
        console.log('loadTeacherList');
        let url = 'http://localhost:9800/teacher';
        console.log('url = ' + url);
        this.axios.get(url).then((response) => {
          let responseBody = response.data;
          this.teacherListOptions = responseBody.data;
        });
      },
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {  /*对表单进行检查验证*/
          if (valid) {
            this.loadMajorListByFacultyId();
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    },
    mounted() {  //挂载，在created后面执行的
      console.log('mounted');
      this.loadMajorList();
      this.loadFacultyList();
      this.loadTeacherList();
    }
  }
</script>
